package io.renren.modules.sys.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.entity.TalentsEntity;
import io.renren.modules.sys.entity.TalentsLogEntity;
import io.renren.modules.sys.service.TalentsLogService;
import io.renren.modules.sys.service.TalentsService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-12-19 23:56:37
 */
@RestController
@RequestMapping("sys/talents")
public class TalentsController extends AbstractController {
    @Autowired
    private TalentsService talentsService;
    
    @Autowired
    private TalentsLogService talentsLogService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
    	SysUserEntity sysUserEntity=getUser();
    	params.put("userId", sysUserEntity.getUserId());
        PageUtils page = talentsService.queryPage(params);

        return R.ok().put("page", page);
    }
    
    /**
     * 列表
     */
    @RequestMapping("/list2")
    public R list2(@RequestParam Map<String, Object> params){
        PageUtils page = talentsService.queryPage(params);

        return R.ok().put("page", page);
    }
    
    /**
     * 公共人才列表
     */
    @RequestMapping("/list3")
    public R list3(@RequestParam Map<String, Object> params){
    	params.put("com", 1);
        PageUtils page = talentsService.queryPage(params);

        return R.ok().put("page", page);
    }
    
    /**
     * 待删除列表
     */
    @RequestMapping("/list4")
    public R list4(@RequestParam Map<String, Object> params){
    	SysUserEntity sysUserEntity=getUser();
    	params.put("userId", sysUserEntity.getUserId());
        PageUtils page = talentsService.queryPage2(params);
        return R.ok().put("page", page);
    }
    
    /**
     * 我的收藏
     */
    @RequestMapping("/list5")
    public R list5(@RequestParam Map<String, Object> params){
    	SysUserEntity sysUserEntity=getUser();
    	params.put("userId", sysUserEntity.getUserId());
        PageUtils page = talentsService.queryPage3(params);
        return R.ok().put("page", page);
    }
    
    /**
     * 被收藏列表
     */
    @RequestMapping("/list6")
    public R list6(@RequestParam Map<String, Object> params){
    	SysUserEntity sysUserEntity=getUser();
    	params.put("userId", sysUserEntity.getUserId());
        PageUtils page = talentsService.queryPage4(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
        TalentsEntity talents = talentsService.selectById(id);

        return R.ok().put("talents", talents);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody TalentsEntity talents){
    	SysUserEntity sysUserEntity=getUser();
    	talents.setUserId(sysUserEntity.getUserId().intValue());
    	talents.setUserName(sysUserEntity.getUsername());
    	talents.setAddTime(new Date());
    	talents.setUpdTime(new Date());
    	Map<String, Object> params=new HashMap<>();
		params.put("phone", talents.getPhone());
		if(talentsService.queryPage(params).getList().size()>0){
			return R.error("人才已存在");
		}
		Map<String, Object> params2=new HashMap<>();
    	params2.put("userId", sysUserEntity.getUserId());
        PageUtils page = talentsService.queryPage(params2);
        if(page.getTotalCount()>=sysUserEntity.getNum2()) {
        	 talents.setCom(1);
			 talents.setUserId(0);
			 talents.setUserName("");
        }
        talentsService.insert(talents);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody TalentsEntity talents){
        ValidatorUtils.validateEntity(talents);
        talents.setUpdTime(new Date());
        talentsService.updateAllColumnById(talents);//全部更新
        TalentsLogEntity talentsLog=new TalentsLogEntity();
        SysUserEntity sysUserEntity=getUser();
    	talentsLog.setAddDate(new Date());
    	talentsLog.setUserId(sysUserEntity.getUserId().intValue());
    	talentsLog.setUserName(sysUserEntity.getName());
    	talentsLog.setTalentsId(talents.getId());
    	talentsLog.setContent(sysUserEntity.getName()+"：更新资料");
        talentsLogService.insert(talentsLog);
        return R.ok();
    }
    
    /**
     * 释放公共库
     */
    @RequestMapping("/update2")
    public R update2(@RequestBody Integer[] ids){
		for (Integer id : ids) {
			 TalentsEntity talents = talentsService.selectById(id);
			 talents.setCom(1);
			 talents.setUserId(0);
			 talents.setUserName("");
			talentsService.updateAllColumnById(talents);//全部更新
	        TalentsLogEntity talentsLog=new TalentsLogEntity();
			SysUserEntity sysUserEntity=getUser();
	    	talentsLog.setAddDate(new Date());
	    	talentsLog.setUserId(sysUserEntity.getUserId().intValue());
	    	talentsLog.setUserName(sysUserEntity.getName());
	    	talentsLog.setTalentsId(talents.getId());
	    	talentsLog.setContent(sysUserEntity.getName()+"：释放到公共库");
	        talentsLogService.insert(talentsLog);
		}        
        return R.ok();
    }
    
    /**
     * 我要收藏
     */
    @RequestMapping("/update4")
    public R update4(@RequestBody Integer[] ids){
    	SysUserEntity sysUserEntity=getUser();
		for (Integer id : ids) {
			 TalentsEntity talents = talentsService.selectById(id);
			 talents.setCollection(sysUserEntity.getUserId().intValue());
			 talents.setCollectionStatus(1);
			talentsService.updateAllColumnById(talents);//全部更新
			TalentsLogEntity talentsLog=new TalentsLogEntity();
	    	talentsLog.setAddDate(new Date());
	    	talentsLog.setUserId(sysUserEntity.getUserId().intValue());
	    	talentsLog.setUserName(sysUserEntity.getName());
	    	talentsLog.setTalentsId(talents.getId());
	    	talentsLog.setContent(sysUserEntity.getName()+"：收藏人才");
	        talentsLogService.insert(talentsLog);
		}        
        return R.ok();
    }
    
    /**
     * 同意收藏
     */
    @RequestMapping("/update5")
    public R update5(@RequestBody Integer[] ids){
		for (Integer id : ids) {
			 TalentsEntity talents = talentsService.selectById(id);
			 talents.setCollectionStatus(1);
			talentsService.updateAllColumnById(talents);//全部更新
		}        
        return R.ok();
    }
    
    /**
     * 我要删除	
     */
    @RequestMapping("/update3")
    public R update3(@RequestBody Integer[] ids){
		for (Integer id : ids) {
			 TalentsEntity talents = talentsService.selectById(id);
			 talents.setIsDel(1);
			talentsService.updateAllColumnById(talents);//全部更新
		}        
        return R.ok();
    }
    
    
    /**
     * 取消收藏
     */
    @RequestMapping("/update6")
    public R update6(@RequestBody Integer[] ids){
    	SysUserEntity sysUserEntity=getUser();
		for (Integer id : ids) {
			 TalentsEntity talents = talentsService.selectById(id);
			 talents.setCollection(0);
			 talents.setCollectionStatus(0);
			talentsService.updateAllColumnById(talents);//全部更新
			TalentsLogEntity talentsLog=new TalentsLogEntity();
	    	talentsLog.setAddDate(new Date());
	    	talentsLog.setUserId(sysUserEntity.getUserId().intValue());
	    	talentsLog.setUserName(sysUserEntity.getName());
	    	talentsLog.setTalentsId(talents.getId());
	    	talentsLog.setContent(sysUserEntity.getName()+"：取消收藏人才");
	        talentsLogService.insert(talentsLog);
		}        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
        talentsService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
